package com.guaju.yueshangLife.activity.mine;

import android.content.res.AssetFileDescriptor;
import android.databinding.DataBindingUtil;
import android.graphics.ColorSpace;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.bean.AboutUsBean;
import com.guaju.yueshangLife.bean.AdviceBean;
import com.guaju.yueshangLife.databinding.ActivityAdviceBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.ProgressUtil;
import com.guaju.yueshangLife.utils.ToastUtil;
import com.guaju.yueshangLife.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.io.InputStream;

import xyz.bboylin.universialtoast.UniversalToast;

public class AdviceActivity extends BaseActivity {

    ActivityAdviceBinding binding;
    int type = 0;

    @Override
    protected void loadViewLayout() {
        EventBus.getDefault().register(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_advice);
    }

    @Override
    protected void bindViews() {
        binding.radio1.setRadioChecked(true);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    protected void setListener() {
        binding.radio1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.radio1.setRadioChecked(true);
                type = 0;
                binding.radio2.setRadioChecked(false);
                binding.radio3.setRadioChecked(false);
            }
        });
        binding.radio2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.radio2.setRadioChecked(true);
                type = 1;
                binding.radio1.setRadioChecked(false);
                binding.radio3.setRadioChecked(false);
            }
        });
        binding.radio3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.radio3.setRadioChecked(true);
                type = 2;
                binding.radio1.setRadioChecked(false);
                binding.radio2.setRadioChecked(false);
            }
        });

        binding.topbar.getRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 提交信息
                 */
                String content = binding.etAdvice.getText().toString().trim();
                String email = binding.etEmail.getText().toString().trim();
                String other = binding.etOthers.getText().toString().trim();

                if (TextUtils.isEmpty(content)) {
                    ToastUtil.show("意见内容不能为空");
                } else if (TextUtils.isEmpty(email)) {
                    ToastUtil.show("邮箱地址不能为空");
                } else if (!Utils.checkEmail(email)) {
                    ToastUtil.show("邮箱地址格式有误");
                } else {
                    ProgressUtil.showProgressDialog(AdviceActivity.this, "提交中");
                    ProgressUtil.setUnCancelable();
                    ApiEntity.advice(content, type, "", email, other);
                }
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAbout(AdviceBean data) {
        if (data != null) {
            int code = data.getCode();
            if (code == 200) {
                UniversalToast.makeText(AdviceActivity.this, "已提交", UniversalToast.LENGTH_SHORT, UniversalToast.EMPHASIZE)
                        .setGravity(Gravity.CENTER, 0, 0)
                        .setBackground(getResources().getDrawable(R.drawable.shape_toast_bg)) // 设置背景
                        .setLeftIconRes(R.drawable.sucess_toast)
                        .show();
                ProgressUtil.dismissProgressDialog();
                finish();
            } else {
                String msg = data.getMsg();
                UniversalToast.makeText(AdviceActivity.this, msg, UniversalToast.LENGTH_SHORT, UniversalToast.EMPHASIZE)
                        .setGravity(Gravity.CENTER, 0, 0)
                        .setBackground(getResources().getDrawable(R.drawable.shape_toast_bg)) // 设置背景
                        .setLeftIconRes(R.drawable.sucess_toast)
                        .show();
                ProgressUtil.dismissProgressDialog();
            }

        } else {
            ToastUtil.show("获取数据错误,请稍后重试");
            finish();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
